package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.pipeline;/**
 * Created by Administrator on 2017/3/13.
 */

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.PipelineMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Organization;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Pipeline;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.organization.JsmOrganizationService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.pipeline.JsmPipelineService;
import com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.util.OrgLevelUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author 卢曼成
 * @Description 管线接口实现类
 * @CreateDateTime 2017-03-13 11:37
 **/

public class JsmPipelineServiceImpl implements JsmPipelineService {
    @Autowired
    private PipelineMapper pipelineMapper;
    @Autowired
    private JsmOrganizationService organizationService;

    /**
     * 根据Id物理删除
     *
     * @param eqptId
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int deleteByPrimaryKey(String eqptId) {
        return pipelineMapper.deleteByPrimaryKey(eqptId);
    }

    /**
     * 根据ID逻辑删除
     *
     * @param eqptId
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int updateStatus(String eqptId) {
        return pipelineMapper.updateStatus(eqptId);
    }

    /**
     * 新增
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int insert(Pipeline record) {
        return pipelineMapper.insert(record);
    }

    /**
     * 非空新增
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int insertSelective(Pipeline record) {
        return pipelineMapper.insertSelective(record);
    }

    /**
     * 根据ID查询
     *
     * @param eqptId
     * @return Pipeline
     * @Author 卢曼成
     */
    @Override
    public Pipeline selectByPrimaryKey(String eqptId) {
        return pipelineMapper.selectByPrimaryKey(eqptId);
    }

    /**
     * 根据ID非空修改
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int updateByPrimaryKeySelective(Pipeline record) {
        return pipelineMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 修改
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int updateByPrimaryKey(Pipeline record) {
        return pipelineMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据条件查询集合
     *
     * @param record
     * @return List<Pipeline>
     * @Author 卢曼成
     */
    @Override
    public List<Pipeline> selectByPrimaryList(Pipeline record) {
        return pipelineMapper.selectByPrimaryList(record);
    }

    /**
     * @Author 卢曼成
     * @Description 添加修改
     * @Date 2017/3/9 15:23
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int saveOrUpdate(Pipeline record, Employee employee) throws Exception {
        String userId = employee.getEmployeeId();
        Date date = new Date();
        record.setBsflag("0");
        Organization orgs = organizationService.findOrgById(record.getOrgId());
        Organization org = new Organization();
        if (orgs != null) {
            record.setOrgId(orgs.getOrgId());
            org.setParentOrgId(orgs.getOrgId());
        } else {
            throw new Exception("管线上级组织错误");
        }
        int num;
        org.setOrgName(record.getPipelineName());
        if (StringUtils.isNotEmpty(record.getEqptId())) {
            org.setOrgId(record.getEqptId());
            org.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
            org.setOperateId(userId);
            //管线数据
            record.setUpdateDate(date);
            record.setUpdateUserId(userId);
            organizationService.updateByPrimaryKeySelective(org);
            num = updateByPrimaryKey(record);
        } else {
            String id = UniqueUtil.uuid();
            org.setOrgId(id);
            org.setCreateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
            org.setCreatorId(userId);
            org.setOrgLevelId(OrgLevelUtils.ORG_LEVEL_PIPELINE);
            record.setCreateDate(date);
            record.setCreateUserId(userId);
            record.setEqptId(id);
            organizationService.insert(org);
            num = this.insert(record);
        }
        return num;
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量添加
     * @Date 2017/2/8 17:17
     */
    @Override
    public int insertList(List<Pipeline> list) {
        return pipelineMapper.insertList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<Pipeline> list) {
        return pipelineMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/8 17:18
     */
    @Override
    public int deleteList(List<Pipeline> list) {
        return pipelineMapper.deleteList(list);
    }
}
